// $Id$
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Globalization;
using Salamanca.DataAccess;
using Salamanca.DataAccess.Collections;
using Salamanca.DataAccess.Data;

namespace Northwind.DomainModel.Data
{

    /// <summary />
    public partial class CategoryMapper
    {

        /// <summary />
        static CategoryMapper()
        {
            Resources.Sql.Culture=CultureInfo.InvariantCulture;
        }

        /// <summary />
        private DomainModelKeyedCollection<Category> _FindAll(DataMapperCollection dataMappers)
        {
            IDbCommand[] commands=new IDbCommand[1];

            commands[0]=Connection.CreateCommand();
            commands[0].CommandType=CommandType.Text;
            commands[0].CommandText=Resources.Sql.CategorySelectAll;

            return Load(commands, dataMappers);
        }

        private Category.DataTransfer _GetDataTransferObject(IList<IDataRecord> records)
        {
            Category.DataTransfer data=new Category.DataTransfer();

            return FillData(data, records[0]);
        }

        /// <summary />
        internal static Category.DataTransfer FillData(Category.DataTransfer data, IDataRecord record)
        {
            int ord=record.GetOrdinal("CategoryID");
            if (!record.IsDBNull(ord))
                data.CategoryID=record.GetInt32(ord);

            ord=record.GetOrdinal("CategoryName");
            if (!record.IsDBNull(ord))
                data.CategoryName=record.GetString(ord);

            ord=record.GetOrdinal("Description");
            if (!record.IsDBNull(ord))
                data.Description=record.GetString(ord);

            return data;
        }

        /// <summary />
        private static IDbCommand _CreateSelectCommand(IDbConnection connection, ParameterBuilder parameterBuilder, Category.PrimaryKey key)
        {
            IDbCommand command=connection.CreateCommand();
            command.CommandType=CommandType.Text;
            command.CommandText=string.Format(
                CultureInfo.InvariantCulture,
                Resources.Sql.CategorySelect,
                parameterBuilder.GetMarker("CategoryID", 1)
            );

            IDbDataParameter p=command.CreateParameter();
            p.ParameterName=parameterBuilder.GetName("CategoryID", 1);
            p.DbType=DbType.Int32;
            p.Value=key.Value;
            p.Direction=ParameterDirection.Input;
            command.Parameters.Add(p);

            return command;
        }

        /// <summary />
        private static IDbCommand _CreateDeleteCommand(IDbConnection connection, ParameterBuilder parameterBuilder, Category.DataTransfer data)
        {
            IDbCommand command=connection.CreateCommand();
            command.CommandType=CommandType.Text;
            command.CommandText=string.Format(
                CultureInfo.InvariantCulture,
                Resources.Sql.CategoryDelete,
                parameterBuilder.GetMarker("CategoryID", 1)
            );

            IDbDataParameter p=command.CreateParameter();
            p.ParameterName=parameterBuilder.GetName("CategoryID", 1);
            p.DbType=DbType.Int32;
            p.Value=data.CategoryID;
            p.Direction=ParameterDirection.Input;
            command.Parameters.Add(p);

            return command;
        }

        /// <summary />
        private static IDbCommand _CreateInsertCommand(IDbConnection connection, ParameterBuilder parameterBuilder, Category.DataTransfer data)
        {
            IDbCommand command=connection.CreateCommand();
            command.CommandType=CommandType.Text;
            command.CommandText=string.Format(
                CultureInfo.InvariantCulture,
                Resources.Sql.CategoryInsert,
                parameterBuilder.GetMarker("CategoryID", 1),
                parameterBuilder.GetMarker("CategoryName", 2),
                parameterBuilder.GetMarker("Description", 3)
            );

            IDbDataParameter p=command.CreateParameter();
            p.ParameterName=parameterBuilder.GetName("CategoryID", 1);
            p.DbType=DbType.Int32;
            p.Value=data.CategoryID;
            p.Direction=ParameterDirection.Input;
            command.Parameters.Add(p);

            p=command.CreateParameter();
            p.ParameterName=parameterBuilder.GetName("CategoryName", 2);
            p.DbType=DbType.String;
            p.Value=data.CategoryName;
            p.Size=15;
            p.Direction=ParameterDirection.Input;
            command.Parameters.Add(p);

            p=command.CreateParameter();
            p.ParameterName=parameterBuilder.GetName("Description", 3);
            p.DbType=DbType.String;
            p.Value=(object)data.Description ?? (object)DBNull.Value;
            p.Size=8192;
            p.Direction=ParameterDirection.Input;
            command.Parameters.Add(p);

            return command;
        }

        /// <summary />
        private static IDbCommand _CreateUpdateCommand(IDbConnection connection, ParameterBuilder parameterBuilder, Category.DataTransfer data)
        {
            IDbCommand command=connection.CreateCommand();
            command.CommandType=CommandType.Text;
            command.CommandText=string.Format(
                CultureInfo.InvariantCulture,
                Resources.Sql.CategoryUpdate,
                parameterBuilder.GetMarker("CategoryName", 1),
                parameterBuilder.GetMarker("Description", 2),
                parameterBuilder.GetMarker("CategoryID", 3)
            );

            IDbDataParameter p=command.CreateParameter();
            p.ParameterName=parameterBuilder.GetName("CategoryName", 1);
            p.DbType=DbType.String;
            p.Value=data.CategoryName;
            p.Size=15;
            p.Direction=ParameterDirection.Input;
            command.Parameters.Add(p);

            p=command.CreateParameter();
            p.ParameterName=parameterBuilder.GetName("Description", 2);
            p.DbType=DbType.String;
            p.Value=(object)data.Description ?? (object)DBNull.Value;
            p.Size=8192;
            p.Direction=ParameterDirection.Input;
            command.Parameters.Add(p);

            p=command.CreateParameter();
            p.ParameterName=parameterBuilder.GetName("CategoryID", 3);
            p.DbType=DbType.Int32;
            p.Value=data.CategoryID;
            p.Direction=ParameterDirection.Input;
            command.Parameters.Add(p);

            return command;
        }

        /// <summary>Gets the parameter builder instance.</summary>
        /// <value>The parameter builder instance.</value>
        protected ParameterBuilder ParameterBuilder
        {
            get
            {
                return _ParameterBuilder;
            }
        }

        private ParameterBuilder _ParameterBuilder;
    }

}
